package com.itheima.controller;

import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
@WebServlet("/brand")
public class BrandController extends HttpServlet {
    private BrandService brandService = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        switch (action){
            case "add":
                this.add(req,resp);
                break;
            case "delect":
                this.delect(req,resp);
                break;
            case "update":
                this.update(req,resp);
                break;
            case "selectById":
                this.selectById(req,resp);
                break;
            case "selectAll":
                this.selectAll(req,resp);
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        Brand brand = new Brand();
        try {
            BeanUtils.populate(brand,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        brandService.addBrand(brand);
        this.selectAll(req,resp);
    }

    private void delect(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        brandService.delectById(id);

        this.selectAll(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        Brand brand = new Brand();
        try {
            BeanUtils.populate(brand,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        brandService.update(brand);
        this.selectAll(req,resp);
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Brand brand = brandService.selectById(id);
        req.setAttribute("brand",brand);
        try {
            req.getRequestDispatcher("/updateBrand.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        List<Brand> brands = brandService.selectAll();
        req.setAttribute("brands",brands);
        try {
            req.getRequestDispatcher("/brandList.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
